<?php
/**
*
* * 作    者：寒川<hanchuan@qiawei.com>
* 日    期：2016-09-20
* 版    本：1.0.0
* 功能说明：文章控制器。
*
**/

namespace Qwadmin\Controller;
use Qwadmin\Controller\ComController;
use Vendor\Tree;

class DeviceController extends ComController {

	public function add(){
		
		$category = M('category')->field('id,pid,name')->order('o asc')->select();
		$tree = new Tree($category);
		$str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
		$category = $tree->get_tree(0,$str,0);
		$this->assign('category',$category);//导航
		$this -> display();
	}
		
	public function index(){
		
	
        $keyword=isset($_POST['keyword'])?$_POST['keyword']:'000';
		$device = M('lock_stock');
		$list  = $device->where("lock_stock.serial_number LIKE '%$keyword%'")->select();	
        $this->assign('list',$list);
       
        	

		$this -> display();
	}
	
	public function del(){
		
		$ls_id = isset($_REQUEST['ls_id'])?$_REQUEST['ls_id']:false;
		if($ls_id){
			if(is_array($ls_id)){
				$ls_id = implode(',',$ls_id);
				$map['ls_id']  = array('in',$ls_id);
			}else{
				$map = 'ls_id='.$ls_id;
			}
			if(M('lock_stock')->where($map)->delete()){
				addlog('删除设备信息：'.$ls_id);
				$this->success('设备删除成功！');
			}else{
				$this->error('参数错误！');
			}
		}else{
			$this->error('参数错误！');
		}

	}
	
	public function edit($ls_id){
		
		$ls_id = intval($ls_id);
		$lock_stock = M('lock_stock')->where('ls_id='.$ls_id)->find();
		if($lock_stock){
			$this->assign('lock_stock',$lock_stock);
		}else{
			$this->error('参数错误！');	
		}
		$this -> display();
	}
	
	public function update(){
		
	    $ls_id = $_POST['ls_id'];
		
		$data['device_uuid'] = isset($_POST['device_uuid'])?$_POST['device_uuid']:false;
		$data['device_address'] = isset($_POST['device_address'])?$_POST['device_address']:false;
        $data['devide_name'] = isset($_POST['devide_name'])?$_POST['devide_name']:false; 
        $data['serial_number']=isset($_POST['serial_number'])?$_POST['serial_number']:false;
        $data['md5_device_uuid']=isset($_POST['md5_device_uuid'])?$_POST['md5_device_uuid']:false;
          	
		if($ls_id){
			M('lock_stock')->data($data)->where('ls_id='.$ls_id)->save();
			addlog('修改设备信息：'.$av_id);
			$this->success('恭喜！设备信息修改成功！');
		}else{
			$adddevice = M('lock_stock')->data($data)->add();
			if($adddevice){
				addlog('新增设备信息：'.$adddevice);
				$this->success('设备信息添加成功');
			}else{
				$this->error('抱歉，未知错误！');
			}
			
		}
	}
}